using System;
using System.Linq;
using System.Diagnostics.Contracts;
using System.Collections.Generic;

namespace ReactiveCode.Expressions
{
    interface IRootExpression
    {
        ReactiveHandle Handle { get; }
        ReactiveHost Host { get; }
        Time ActivationTime { get; }
    }
    internal class RootExpression<T> : MethodCall<T>, IRootExpression
    {
        internal override ReactiveHost Host
        {
            get { return Handle.Host; }
        }
        ReactiveHost IRootExpression.Host
        {
            get { return Host; }
        }
        public ReactiveHandle Handle { get; private set; }

        public Time ActivationTime { get; private set; }

        public RootExpression(ReactiveHandle handle, ReactiveMethodDefinition<T> method)
            : base(method)
        {
            Contract.Requires(handle != null);
            Handle = handle;
            Root = this;
            EnabledProperty.Where(x => x).Subscribe(_ => ActivationTime = Time.Current);
        }

        internal override void OnError(ReactiveUnhandledExceptionEventArgs e)
        {
            base.OnError(e);
            Handle.OnError(e);
        }
    }
}